from django.urls import path,reverse
from django.utils.safestring import mark_safe

from stark.service.v1 import StarkHandler,get_choice_text,get_manytomany,BootStrapModelForm,SearchOption
from web.models import Student

class StudentModelForm(BootStrapModelForm):
    class Meta:
        model=Student
        fields=['qq','mobile','emergency_contract','memo']

class StudentHandler(StarkHandler):
    """
    学生不能随便添加和删除，删除需要特别高的权限，增加呢都是通过销售添加过来的
    """
    model_form_class = StudentModelForm

    def display_score(self,obj=None,is_header=None,*args,**kwargs):
        if is_header:
            return '积分管理'
        record_url=reverse("stark:web_scorerecord_list",kwargs={'student_id':obj.pk})
        return mark_safe("<a target='_blank' href='{}'>{}</a>".format(record_url,obj.score))

    list_display = ['customer','qq','mobile','emergency_contract',get_manytomany('班级','class_list'),
                    get_choice_text('状态','student_status'),display_score]

    search_list = ['customer__name','qq','mobile']
    search_group = [
        SearchOption('class_list', text_func=lambda x: '%s-%s' % (x.school.title, str(x)))
    ]

    def get_add_btn(self,request,*args,**kwargs):
        return None

    def get_list_display(self):
        value=[]
        if self.list_display:
            value.extend(self.list_display)
            value.append(type(self).display_edit)
            # value.insert(0,type(self).display_check)
        return value
    action_list = []

    def get_urls(self):
        patterns=[]
        # print(self.get_list_url_name)
        #self.wapper名字错了，正确的英文写法应该是wrapper
        patterns.append(path('list/',self.wapper(self.list_view),name=self.get_list_url_name))
        patterns.append(path('edit/<int:pk>/',self.wapper(self.change_view),name=self.get_edit_url_name))
        patterns.extend(self.extra_urls())
        return patterns